<template>
	<Layout :class="'bg-white p-2'">
		<Row class="m-1">
			<Button class="float-left m-1" type="error" @click="newAdv">新增</Button>
			<Col class="p-1" span="4" placeholder="请选择状态">
				<Select v-model="searchObject.statu" clearable >
					<Option value="1">激活</Option>
					<Option value="0">禁用</Option>
				</Select>
			</Col>
			<Button class="m-1 float-right" type="primary" shape="circle" @click="search" icon="ios-search">查询</Button>
		</Row>

		<Layout :class="'bg-white'">
			<Table border :class="'m-1'" :columns="tableInfo.columnInfo" :data="tableInfo.data">
				<template slot-scope="{ row, index }" slot="url">
					<img v-if="row.url" style="width: 50px; height: 50px; border-radius: 25px;" :src="'https://'+row.url" />
					<span v-else>无</span>
				</template>
				<template slot-scope="{ row, index }" slot="statu">
					<span v-if="row.statu">激活</span>
					<span v-else>禁用</span>
				</template>
				<template slot-scope="{ row, index }" slot="action">
					<Button type="primary" size="small" style="margin-right: 5px" @click="show(row.advId)">编辑</Button>
					<Button type="error" size="small" @click="remove(row.advId)">删除</Button>
				</template>
			</Table>
			<Page :class="'m-2 text-right'" :page-size="tableInfo.pageSize" @on-change="pageSearch" :total="tableInfo.total" />
		</Layout>
	</Layout>
</template>

<script>
	export default {
		data() {
			return {
					searchObject: {
						advId: "",
						imgId: "",
						statu: "",
						url: "",
					}, 
					tableInfo: {
						currentPage: 1,
						total: 0,
						pageSize: 10,
						columnInfo: [
							{
								title: "广告图片",
								slot: "url",
								width: "96",
							},
							{
								title: "广告状态",
								slot: "statu",
								width: "auto",
							},
							{
								title: "操作",
								slot: 'action',
								width: 200
							}
						],
						data: []
					}
				
			};
		},
		methods: {
			init: function() {
				this.tableInfo.currentPage = 0;
				this.search();
			},
			newAdv: function() {
				this.$router.push({
					name: "adv_new"
				});
			},
			show: function(advId) {
				this.$router.push({
					path: "/adv/detail?advId="+advId,
				});
			},
			search: function() {
				this.http.get({
					url: "/adv/page",
					param: {
						current: this.tableInfo.currentPage,
						statu: this.searchObject.statu,
					}
				}).then(data => {
					this.tableInfo.currentPage = data.current;
					this.tableInfo.total = data.total;
					this.tableInfo.pageSize = data.size;
					this.tableInfo.data = data.records;
				});
			},
			pageSearch: function(changedPage) {
				this.tableInfo.currentPage = changedPage;
				this.search();
			},
			change: function(advId) {
				search();
				this.http.put({
					path: "/adv/info?advId="+advId,
				});
			},
			remove: function(advId) {
				this.message.confirm({
					title: "删除提示",
					content: "即将删除该广告图片，确定吗？",
					onOk: () => {
						this.http.delete({
							url: "/adv/info",
							param: {
								advId:advId,
							}
						}).then(data => {
							this.search();
						});
					},
					onCancel: () => {
						this.message.info('操作已取消');
					}
				});
			},
		},
		created: function() {
			this.init();
		},
	}
</script>

<style>
</style>
